﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbSharp.Framework.TestEngine;
using System.Windows.Forms;
using DbSharp.GuiRunner.ViewItems;
using System.ComponentModel;

namespace DbSharp.GuiRunner.View
{
    class GuiTestStatusNotifier : ITestCaseStatusNotify
    {
        RichTextBox m_OutputTextbox;
        DataGridView m_GridView;
        bool m_IsPerfGrid;

        public GuiTestStatusNotifier(RichTextBox outputTextbox, DataGridView grid, bool isPerfGrid)
        {
            m_OutputTextbox = outputTextbox;
            m_GridView = grid;
            m_IsPerfGrid = isPerfGrid;
        }


        private PerfCaseViewItem GetPerfCaseViewItem(TestRunnerBase testRunner)
        {
            BindingList<PerfCaseViewItem> listCaseVI =(BindingList<PerfCaseViewItem>)m_GridView.DataSource;
            foreach (var viewItem in listCaseVI)
            {
                if (viewItem.TestRunner == testRunner)
                    return viewItem;
            }

            return null;
        }

        private UnitCaseViewItem GetUnitCaseViewItem(TestRunnerBase testRunner)
        {
            BindingList<UnitCaseViewItem> listCaseVI = (BindingList<UnitCaseViewItem>)m_GridView.DataSource;
            foreach (var viewItem in listCaseVI)
            {
                if (viewItem.TestRunner == testRunner)
                    return viewItem;
            }

            return null;
        }

        private string UpdateProgressText(string oldProgressText, CaseRunPhase testPhase)
        {
            string progressText;
            if (testPhase == CaseRunPhase.Finished)
            {
                progressText = "Over";
            }
            else
            { 
                if ((oldProgressText.Length < 6)
                && (string.Equals(oldProgressText, "Over") == false))
                    progressText = oldProgressText + "•";
                else
                    progressText = "•";
            }
            return progressText;
        }

        private void RepaintGrid(TestCaseStatusNotifyEventArgs arg)
        {
            if (m_IsPerfGrid)
            {
                var viewItem = GetPerfCaseViewItem(arg.TestRunner);
                if (viewItem != null)
                {
                    string progressText = UpdateProgressText(viewItem.ProgressText, arg.TestPhase);
                    viewItem.ProgressText = progressText;
                    viewItem.Refresh();
                }
            }
            else
            {
                var viewItem = GetUnitCaseViewItem(arg.TestRunner);
                if (viewItem != null)
                {
                    string progressText = UpdateProgressText(viewItem.ProgressText, arg.TestPhase);
                    viewItem.ProgressText = progressText;
                    viewItem.Refresh();
                }
            }
        }


        #region ITestStatusNotify Members

        public TestRunnerBase TestRunner { get; set; }

  
        public void UpdateStatus(TestCaseStatusNotifyEventArgs arg)
        { 
            if (arg == null)
                return;
            
            //write log
            string messageFmt = "{0}: {1}";
            string message = string.Format(messageFmt, TestRunner.CaseName, arg.ToString());
            AppendText(message + Environment.NewLine);

            RepaintGrid(arg);
        }

        private void AppendText(string message)
        {
            if (m_OutputTextbox.InvokeRequired)
            {
                IAsyncResult ar = m_OutputTextbox.BeginInvoke(new Action<string>(AppendText), message);
                m_OutputTextbox.EndInvoke(ar);
            }
            else
                m_OutputTextbox.AppendText(message);
        }
     

        #endregion

   
        #region IDisposable Members

        public void Dispose()
        { 
        }

        #endregion
 
    }
     

}
